from rest_framework import serializers
from .models import Param
from rest_framework.validators import UniqueValidator
from frame_server.serializers import BaseSerializer as BSerializer

class AddEditBase(BSerializer):
    paramKey = serializers.CharField(
        max_length=255,
        validators=[UniqueValidator(queryset=Param.objects.all(), message="参数键已存在")]
    )
    class Meta:
        model = Param

# 新增
class AddValidate(AddEditBase):
    class Meta(AddEditBase.Meta):
        exclude = ['id']

# 修改
class EditValidate(AddEditBase):
    id = serializers.IntegerField()
    
    def validate_id(self, val):
        if not Param.objects.filter(id=val).exists():
            raise serializers.ValidationError("编辑对象不存在")
        return val

    class Meta(AddEditBase.Meta):
        fields = '__all__'
    